


<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
  "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html id="htmlId">
<head>
  <title>Coverage Report :: ThrowableConverter</title>
  <style type="text/css">
    @import "../../.css/coverage.css";
  </style>
</head>

<body>
<div class="header"></div>

<div class="content">
<div class="breadCrumbs">
    [ <a href="../../index.html">all classes</a> ]
    [ <a href="../index.html">at.schauer.gregor.vienna.convert.vienna.lang</a> ]
</div>

<h1>Coverage Summary for Class: ThrowableConverter (at.schauer.gregor.vienna.convert.vienna.lang)</h1>

<table class="coverageStats">
<tr>
  <th class="name">Class</th>
<th class="coverageStat 
">
  Class, %
</th>
<th class="coverageStat 
">
  Method, %
</th>
<th class="coverageStat 
">
  Line, %
</th>
</tr>
<tr>
  <td class="name">ThrowableConverter</td>
<td class="coverageStat">
  <span class="percent">
    100%
  </span>
  <span class="absValue">
    (1/ 1)
  </span>
</td>
<td class="coverageStat">
  <span class="percent">
    88.9%
  </span>
  <span class="absValue">
    (8/ 9)
  </span>
</td>
<td class="coverageStat">
  <span class="percent">
    81.8%
  </span>
  <span class="absValue">
    (36/ 44)
  </span>
</td>
</tr>

</table>

<br/>
<br/>


<div class="sourceCode"><i>1</i>&nbsp;/*
<i>2</i>&nbsp; * Copyright 2012 Gregor Schauer
<i>3</i>&nbsp; *
<i>4</i>&nbsp; * Licensed under the Apache License, Version 2.0 (the &quot;License&quot;);
<i>5</i>&nbsp; * you may not use this file except in compliance with the License.
<i>6</i>&nbsp; * You may obtain a copy of the License at
<i>7</i>&nbsp; *
<i>8</i>&nbsp; *      http://www.apache.org/licenses/LICENSE-2.0
<i>9</i>&nbsp; *
<i>10</i>&nbsp; * Unless required by applicable law or agreed to in writing, software
<i>11</i>&nbsp; * distributed under the License is distributed on an &quot;AS IS&quot; BASIS,
<i>12</i>&nbsp; * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
<i>13</i>&nbsp; * See the License for the specific language governing permissions and
<i>14</i>&nbsp; * limitations under the License.
<i>15</i>&nbsp; */
<i>16</i>&nbsp;package at.schauer.gregor.vienna.convert.vienna.lang;
<i>17</i>&nbsp;
<i>18</i>&nbsp;import at.schauer.gregor.vienna.convert.vienna.base.AbstractContainerConverter;
<i>19</i>&nbsp;import at.schauer.gregor.vienna.exception.ViennaException;
<i>20</i>&nbsp;import at.schauer.gregor.vienna.serializer.Serializer;
<i>21</i>&nbsp;import at.schauer.gregor.vienna.util.ViennaUtils;
<i>22</i>&nbsp;import com.google.common.collect.Iterators;
<i>23</i>&nbsp;import org.apache.commons.lang3.ClassUtils;
<i>24</i>&nbsp;import org.apache.commons.lang3.reflect.FieldUtils;
<i>25</i>&nbsp;
<i>26</i>&nbsp;import java.util.Iterator;
<i>27</i>&nbsp;
<i>28</i>&nbsp;/**
<i>29</i>&nbsp; * @author Gregor Schauer
<i>30</i>&nbsp; */
<b class="fc"><i>31</i>&nbsp;public class ThrowableConverter&lt;T extends Throwable&gt; extends AbstractContainerConverter&lt;T, StackTraceElement&gt; {</b>
<b class="fc"><i>32</i>&nbsp;	public static final String DIVIDER = Character.valueOf((char) 0).toString();</b>
<i>33</i>&nbsp;
<i>34</i>&nbsp;	public ThrowableConverter() {
<b class="fc"><i>35</i>&nbsp;		super(&quot;„&quot;, &quot;“&quot;);</b>
<b class="fc"><i>36</i>&nbsp;	}</b>
<i>37</i>&nbsp;
<i>38</i>&nbsp;	@Override
<i>39</i>&nbsp;	public T sourceToTarget(String source, Class&lt;? extends T&gt; targetClass) {
<b class="fc"><i>40</i>&nbsp;		String[] parts = getParts(source);</b>
<b class="fc"><i>41</i>&nbsp;		parts[2] = stripContainerSequences(parts[2]);</b>
<b class="fc"><i>42</i>&nbsp;		String[] split = split(parts[2], getSeparator());</b>
<i>43</i>&nbsp;
<b class="fc"><i>44</i>&nbsp;		T instance = createInstance(targetClass, split.length);</b>
<b class="fc"><i>45</i>&nbsp;		StackTraceElement[] stackTrace = new StackTraceElement[split.length];</b>
<i>46</i>&nbsp;
<i>47</i>&nbsp;		try {
<b class="fc"><i>48</i>&nbsp;			FieldUtils.writeField(instance, &quot;stackTrace&quot;, stackTrace, true);</b>
<b class="fc"><i>49</i>&nbsp;			FieldUtils.writeField(instance, &quot;detailMessage&quot;, parts[0], true);</b>
<i>50</i>&nbsp;
<b class="fc"><i>51</i>&nbsp;			if (!parts[1].isEmpty()) {</b>
<b class="fc"><i>52</i>&nbsp;				String value = ViennaUtils.ConverterUtils.valueFromString(parts[1]);</b>
<b class="fc"><i>53</i>&nbsp;				Class&lt;? extends T&gt; type = (Class&lt;? extends T&gt;) ClassUtils.getClass(ViennaUtils.ConverterUtils.typeFromString(parts[1]));</b>
<b class="fc"><i>54</i>&nbsp;				FieldUtils.writeField(instance, &quot;cause&quot;, sourceToTarget(value, type), true);</b>
<i>55</i>&nbsp;			}
<b class="nc"><i>56</i>&nbsp;		} catch (Exception e) {</b>
<b class="nc"><i>57</i>&nbsp;			throw new ViennaException(e);</b>
<b class="fc"><i>58</i>&nbsp;		}</b>
<i>59</i>&nbsp;
<b class="fc"><i>60</i>&nbsp;		for (int i = 0; i &lt; split.length; i++) {</b>
<b class="fc"><i>61</i>&nbsp;			stackTrace[i] = this.deserialize(split[i]);</b>
<i>62</i>&nbsp;		}
<i>63</i>&nbsp;
<b class="fc"><i>64</i>&nbsp;		return instance;</b>
<i>65</i>&nbsp;	}
<i>66</i>&nbsp;
<i>67</i>&nbsp;	protected String[] getParts(String str) {
<b class="fc"><i>68</i>&nbsp;		String[] parts = new String[3];</b>
<b class="fc"><i>69</i>&nbsp;		int pos = str.indexOf(DIVIDER);</b>
<b class="fc"><i>70</i>&nbsp;		parts[0] = str.substring(0, pos);</b>
<b class="fc"><i>71</i>&nbsp;		parts[1] = str.substring(pos + DIVIDER.length(), str.lastIndexOf(DIVIDER));</b>
<b class="fc"><i>72</i>&nbsp;		parts[2] = str.substring(str.lastIndexOf(DIVIDER) + DIVIDER.length());</b>
<b class="fc"><i>73</i>&nbsp;		return parts;</b>
<i>74</i>&nbsp;	}
<i>75</i>&nbsp;
<i>76</i>&nbsp;	@Override
<i>77</i>&nbsp;	public String targetToSource(T target) {
<b class="fc"><i>78</i>&nbsp;		StringBuilder builder = new StringBuilder();</b>
<b class="fc"><i>79</i>&nbsp;		builder.append(target.getMessage()).append(DIVIDER);</b>
<i>80</i>&nbsp;
<b class="fc"><i>81</i>&nbsp;		if (target.getCause() != null) {</b>
<b class="fc"><i>82</i>&nbsp;			builder.append(ViennaUtils.ConverterUtils.type(target.getCause().getClass()));</b>
<b class="fc"><i>83</i>&nbsp;			builder.append(Serializer.CLASS_OBJECT_SEPARATOR);</b>
<b class="fc"><i>84</i>&nbsp;			builder.append(ViennaUtils.SerializationUtils.save(ctx, target.getCause()));</b>
<i>85</i>&nbsp;		}
<b class="fc"><i>86</i>&nbsp;		builder.append(DIVIDER);</b>
<b class="fc"><i>87</i>&nbsp;		builder.append(super.targetToSource(target).replace(StackTraceElement.class.getName() + Serializer.CLASS_OBJECT_SEPARATOR, &quot;&quot;));</b>
<b class="fc"><i>88</i>&nbsp;		return builder.toString();</b>
<i>89</i>&nbsp;	}
<i>90</i>&nbsp;
<i>91</i>&nbsp;	@Override
<i>92</i>&nbsp;	protected void add(T instance, int i, StackTraceElement elem) {
<i>93</i>&nbsp;		// This method is not called by this implementation.
<i>94</i>&nbsp;		try {
<b class="nc"><i>95</i>&nbsp;			StackTraceElement[] stackTrace = (StackTraceElement[]) FieldUtils.readField(instance, &quot;stackTrace&quot;, true);</b>
<b class="nc"><i>96</i>&nbsp;			stackTrace[i] = elem;</b>
<b class="nc"><i>97</i>&nbsp;		} catch (IllegalAccessException e) {</b>
<b class="nc"><i>98</i>&nbsp;			throw new RuntimeException(e);</b>
<b class="nc"><i>99</i>&nbsp;		}</b>
<b class="nc"><i>100</i>&nbsp;	}</b>
<i>101</i>&nbsp;
<i>102</i>&nbsp;	@Override
<i>103</i>&nbsp;	@SuppressWarnings(&quot;unchecked&quot;)
<i>104</i>&nbsp;	protected &lt;E&gt; E deserialize(String str) {
<b class="fc"><i>105</i>&nbsp;		return (E) ViennaUtils.DeserializationUtils.load(ctx, StackTraceElement.class, str);</b>
<i>106</i>&nbsp;	}
<i>107</i>&nbsp;
<i>108</i>&nbsp;	@Override
<i>109</i>&nbsp;	protected Iterator&lt;StackTraceElement&gt; getElements(T target) {
<b class="fc"><i>110</i>&nbsp;		return Iterators.forArray(target.getStackTrace());</b>
<i>111</i>&nbsp;	}
<i>112</i>&nbsp;}
</div>
</div>

<div class="footer">
    
    <div style="float:right;">generated on 2012-07-17 20:23</div>
</div>
</body>
</html>
